package cn.jy.careuplusdoctor.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import cn.jy.careuplusdoctor.R;
import cn.jy.careuplusdoctor.base.BaseActivity;
import cn.jy.careuplusdoctor.http.UploadManager;
import cn.jy.careuplusdoctor.injector.IdInject;
import cn.jy.careuplusdoctor.utils.BitmapUtil;
import cn.jy.careuplusdoctor.utils.CheckNewVersion;
import cn.jy.careuplusdoctor.utils.PopupWindowCommon;
import cn.jy.careuplusdoctor.utils.ProgressMultipartEntity.ProgressCallback;
import cn.jy.careuplusdoctor.utils.StringUtil;
import cn.jy.careuplusdoctor.widgets.CareuTitleBar.TitleBarLeftAction;
import cn.jy.careuplusdoctor.widgets.CareuTitleBar.TitleBarRightAction;

public class LicenceActivity extends BaseActivity  implements TitleBarRightAction,TitleBarLeftAction{
	
	@IdInject(id=R.id.edit_licenceNum)
	private EditText licenceNum;
	@IdInject(id=R.id.txt_photo)
	private TextView photo;
	@IdInject(id=R.id.img_licence)
	private ImageView licence;
	
	private PopupWindowCommon	chooseImgPopup;
	
	/**
	 * 回调CODE:调用相机
	 */
	public static final int	REQUEST_CODE_CAMERA = 9011;
	/**
	 * 回调CODE:调用图片选择
	 */
	public static final int	REQUEST_CODE_CHOOSE_IMG	= 9012;
	/**
	 * 回调CODE:调用图片加截图选择
	 */
	public static final int	REQUEST_CODE_CHOOSE_CUT_IMG	= 9013;
	File file = null;
	String _name = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_licence);
		
		initTitleBar();
		onInJectView();
		chooseImgPopup = new PopupWindowCommon(this).initChooseImg();
	}
	
	@Override
	protected void onAfterInJector() {
		super.onAfterInJector();
		
		OnClickListener listener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.txt_photo:
					chooseImgPopup.showChooseImg(photo);
					break;
				}
			}
		};
		findViewById(R.id.txt_photo).setOnClickListener(listener);
		
		
	}
	
	@Override
	protected void settingTitleBar() {
		super.settingTitleBar();
		
		getTitleBar().setLeftText("注册");
		getTitleBar().setTitle("上传执业证书");
		getTitleBar().setRightText("完成");
		getTitleBar().setLeftAction(this);
		getTitleBar().setRightAction(this);
		getTitleBar().getLeftImageView().setImageResource(R.drawable.title_bar_back);
		getTitleBar().getRightImageView().setVisibility(View.GONE);
	}
	
	@Override
	public void onLeftPress(View view) {
		defaultFinish();
		
	}
	@Override
	public void onRightPress(View view) {
		update();
	}

	private void update() {
		if(file!=null&& StringUtil.isNotEmpty(licenceNum.getText().toString().trim())){
			showLoading("正在提交");
			ProgressCallback _progressCallback = new ProgressCallback() {
				@Override
				public void progress(long percentage) {
				}
			};
			UploadManager.Callback<String> _callback = new UploadManager.Callback<String>() {
				@Override
				public void callback(String rst) {
					hidenLoading();
					JSONObject _json;
					try {
						_json = new JSONObject(rst);
						String resType = _json.getString("resType");
						if (resType.equals("1")) {
							String url = _json.getString("url");
			                Intent intent = new Intent(LicenceActivity.this,RegisterActivity.class);
							Bundle bundle = new Bundle();
							bundle.putString("url", url);
							bundle.putString("licenceNum", licenceNum.getText().toString().trim());
							intent.putExtras(bundle);
							setResult(1001, intent);
							finish();
						} else {
							shortToast("提交失败,请重新提交");
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			};
			new UploadManager().upload(file, _name, "check", _callback,
					_progressCallback);
			
		}else{
			shortToast("请填写执业证书编号并选择照片");
		}
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		if (REQUEST_CODE_CAMERA == requestCode) {
			File _file = new File(CheckNewVersion.FILE_SAVE_DIR + "temp.jpg");
			if (_file.exists()) {
				_name = new Date().getTime() + ".jpg";
				String _fileName = CheckNewVersion.FILE_SAVE_DIR + _name;
				_file.renameTo(new File(_fileName));
				file= new File(_fileName);
				try {
					// 修改图片大小，减少内存
					Bitmap _bitmap = BitmapUtil.zoomImage(BitmapFactory.decodeStream(new FileInputStream(file)), 800);
					licence.setImageBitmap(_bitmap);
					//userManager(BitmapUtil.saveFile(_bitmap, file2));
					chooseImgPopup.hide();
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
		} else if (REQUEST_CODE_CHOOSE_IMG == requestCode && intent != null) {
			Uri _url = intent.getData();
			String[] _filePathColumn = { MediaStore.Images.Media.DATA };
			Cursor _cursor = this.getContentResolver().query(_url, _filePathColumn, null, null, null);
			_cursor.moveToFirst();
			int _columnIndex = _cursor.getColumnIndex(_filePathColumn[0]);
			String _picturePath = _cursor.getString(_columnIndex);
			_name = new Date().getTime() + ".jpg";
			file = new File(_picturePath);
			_cursor.close();
			Bitmap _bitmap = BitmapFactory.decodeFile(_picturePath);
			// 修改图片大小，减少内存
			_bitmap = BitmapUtil.zoomImage(_bitmap, 800);
				//userManager(BitmapUtil.saveFile(_bitmap, _file));
			licence.setImageBitmap(_bitmap);
			chooseImgPopup.hide();
		}/*else if (REQUEST_CODE_CHOOSE_CUT_IMG == requestCode && intent != null) {
			File _file = new File(CheckNewVersion.FILE_SAVE_DIR + new Date().getTime() + ".jpg");
			Bitmap _bitmap = BitmapFactory.decodeFile(CheckNewVersion.FILE_SAVE_DIR + "cut_temp.jpg");
			_bitmap = BitmapUtil.zoomImage(_bitmap, DensityUtil.dip2px(this, 150), DensityUtil.dip2px(this, 210));
			//userManager(BitmapUtil.saveFile(_bitmap, _file));
			chooseImgPopup.hide();
		}*/
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && chooseImgPopup.isShowing()) {
			chooseImgPopup.hide();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}

}
